'use strict';

define(['controllers', 'codemirror', 'codemirror-xml', 'codemirror-javascript', '../service/queryService'], function (controllers, CodeMirror) {
    controllers.collection.register('QueryController', ['$scope', '$routeParams', 'QueryService', function ($scope, $routeParams, queryService) {
        $scope.collectionId = $routeParams.id;
        $scope.init = function () {
            $scope.query_q = '*:*';
            $scope.query_fq = '';
            $scope.query_sort = '';
            $scope.query_start = 0;
            $scope.query_row = 10;
            $scope.query_fl = '';
            $scope.query_wt = 'json';
            $scope.query_hl = false;
            $scope.query_hl_fl = '';
            $scope.query_hl_simple_pre = '';
            $scope.query_hl_simple_post = '';
            $scope.query_spellcheck = false;
            $scope.query_spellcheck_directory = '';
            $scope.query_spellcheck_count = '';
            $scope.query_spellcheck_onlyMorePopular = false;
            $scope.query_spellcheck_collate = false;

            $scope.quering = false;

            setTimeout(function () {
                $scope.result_editor = CodeMirror.fromTextArea(document.getElementById("result_editor"), {
                    mode: "application/json",
                    styleActiveLine: true,
                    lineNumbers: true,
                    lineWrapping: true,
                    readOnly: true
                });
            }, 10);
        };

        $scope.query = function () {
            $scope.quering = true;
            queryService.query($scope.collectionId, {
                q: $scope.query_q,
                fq: $scope.query_fq,
                sort: $scope.query_sort,
                start: $scope.query_start,
                row: $scope.query_row,
                fl: $scope.query_fl,
                wt: $scope.query_wt,
                hl: $scope.query_hl,
                hlFl: $scope.query_hl_fl,
                hlSimplePre: $scope.query_hl_simple_pre,
                hlSimplePost: $scope.query_hl_simple_post,
                spellcheck: $scope.query_spellcheck,
                spellcheckDirectory: $scope.query_spellcheck_directory,
                spellcheckCount: $scope.query_spellcheck_count,
                spellcheckOnlyMorePopular: $scope.query_spellcheck_onlyMorePopular,
                spellcheckCollate: $scope.query_spellcheck_collate
            }, function(data) {
                if ($scope.query_wt == 'json') {
                    $scope.result_editor.setOption('mode', 'application/json');
                } else {
                    $scope.result_editor.setOption('mode', 'application/xml');
                }
                $scope.result_editor.setValue(data);
                setTimeout(function() {
                    $scope.$apply(function () {
                        $scope.quering = false;
                    });
                }, 1000)
            });
        };

        $scope.init();
    }]);
});